import {Component} from '@angular/core';
import {
    AlertController, LoadingController, NavController, NavParams, Platform,
    ToastController, ViewController, ModalController, Modal
} from 'ionic-angular';
import {PanDianMobilePage} from "../propertyManage/pan-dian-mobile/pan-dian-mobile";
import {BarcodeScanner} from "@ionic-native/barcode-scanner";
import {SystemService} from "../service/SystemService";
import {BasePage} from "../BasePage";
import {Log} from "../../framework/utils/Log";
import {ExamineApprovePage} from "../examine/examineApprove";
import {ZxInformationPage} from "../examineApprove/zxInformation/zxInformation";
import {ZjInformationPage} from "../examineApprove/zjInformation/zjInformation";
import {CgInformationPage} from "../examineApprove/cgInformation/cgInformation";
import {HtbgInformationPage} from "../examineApprove/htbgInformation/htbgInformation";
import {HtqdInformationPage} from "../examineApprove/htqdInformation/htqdInformation";
import {ZChanCardInfoPage} from "../zChanCardInfo/zChanCardInfo";
import {BillVerifyPage} from "../billVerify/billVerify";

/**
 * Generated class for the ToolsTabPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

//@IonicPage()
@Component({
    selector: 'page-tools-tab',
    templateUrl: 'tools-tab.html',
})
export class ToolsTabPage  extends BasePage{
    private static TAG: string = 'ToolsTabPage';
    type:string="1";
     pandianStyle: any = [
        {url: './assets/imgs/zichanpandian.png', bgcolor: '#327ed2', text: '资产盘点'},
        {url: './assets/imgs/fapiaoyanz.png', bgcolor: '#3189fb', text: '发票验证'},
        {url: './assets/imgs/zichankapiansaoma.png', bgcolor: '#3be3dd', text: '资产扫码'},
        {url: './assets/imgs/danjusaoma.png', bgcolor: '#e0ba3d', text: '单据扫码'},
        {url: './assets/imgs/saomashangchuan.png', bgcolor: '#e08738', text: '扫码上传'}
    ];
    ziChanScan:any;
    faPiaoVal:any;
    faPiaoBool:boolean=false;
    faPiaoArr=[];
    private profileModal:Modal = null;

    constructor(public platform: Platform,
                public navCtrl: NavController,
                public navParams: NavParams,
                public sys: SystemService,
                public loadingCtrl: LoadingController,
                public toastCtrl: ToastController,
                private barcodeScanner: BarcodeScanner,
                public alertCtrl: AlertController,
                public modalCtrl:ModalController,
                public view:ViewController) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);

    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad ToolsTabPage');
    }

    bgConClick() {
        if (this.view) {
            this.view.dismiss(null, '', {animate: false});
        }
        event.stopPropagation();
    }

    toolsHandler(id, e) {
        switch (id) {
            case 0:
                this.navCtrl.push(PanDianMobilePage);
                break;
            case 1:

                //发票验证
              /*  let obj={
                    msgCode: "1",
                        msg: "成功",
                BaoXiaoBH: "报销单编号",
                BaoXiaoRQ: "报销日期",
                JingBanR: "经办人",
                ShiFouBX: "TRUE"
        }
                var str ="01,10,031001800311,20593055,5034.34,20190521,67740884352190090408,EAAC,";

                var arr =str.split(',');
                this.billVerity(arr,obj);
*/
                this.scanCode(1);
                break;
            case 2:
                this.scanCode(2);
                break;
            case 3:
                //单据扫码
                this.scanCode(3);
                break;
            case 4:
                //扫码上传
                this.scanCode(4);
                break;
        }
        event.stopPropagation();
    }
    //扫码
    scanCode(id){
        this.barcodeScanner.scan().then(barcodeData => {
            if(id===4){
               window.open(barcodeData.text);
            }else if(id===3){
                let data: any = {
                    BianHao: barcodeData.text,
                    LiuChengId: '',
                    RenWuID: '',
                    DangQianJD: ''
                };
                let str =barcodeData.text;
                str = str.substr(0,4);
                if(str=="BXBH"){//报销单
                    let item: any = {
                        DanJuBH: barcodeData.text,
                        LiuChengId:'',
                        RenWuID: '',
                        DangQianJD: ''
                    };
                    this.navCtrl.push(ExamineApprovePage, { data: item, type:'2' });

                }else if(str==="JKDH"){//借款单
                    this.getZjCheckDetail(data,'3');

                }else if(str==="ZXSQ"){//执行申请
                    this.getZxCheckDetail(data,'2');

                }else if(str==="HTDJ"){//合同
                    this.getHtgldHbspDetail(data,'5');

                }else if(str==="HTBG"){//合同变更
                    this.getHtgldbspBgdetail(data,'6');

                }else if(str==="XQHZ"){//采购申请
                    this.getCgxqdbspDetail(data,'4');

                }

            }else if(id===2){
                this.sys.ZiChanXiangXiXXService({ZiChanKPBH:barcodeData.text}).subscribe((res:any)=>{
                    if (res.msgCode === "1"){
                       this.ziChanScan = res.list;
                        let profileModal = this.modalCtrl.create(ZChanCardInfoPage, {list: this.ziChanScan});

                        profileModal.present();
                    }
                })
            }else if(id===1){
                let str = barcodeData.text;
                this.faPiaoArr =str.split(',');
                //let a1 = [arr[0],arr[1],arr[2]];
                this.sys.FaPiaoYZService({FaPiaoHM:this.faPiaoArr[3],FaPiaoDM:this.faPiaoArr[2]}).subscribe((res:any)=>{
                    if (res.msgCode === "1"){
                        this.faPiaoVal = res;
                        this.bgConClick();
                        this.billVerity(this.faPiaoArr,this.faPiaoVal);
                    }else {
                        this.showError('发票验证失败');
                    }
                })
            }

        }, (err) => {
            //console.log('Error: ', err);
        });

    }
    billVerity(arr,data){
        if(this.profileModal) {
            return;
        }
        this.profileModal = this.modalCtrl.create(BillVerifyPage,{bill:arr,data:data});
        this.profileModal.onDidDismiss(async () => {
            this.profileModal = null;
        });
        this.profileModal.present();
    }
    /**
     * 审批详情(合同变更)
     * */
    getHtgldbspBgdetail(data,spType){
        this.sys.htgldbspBgdetail(data).subscribe((res: any) => {
            Log.printObj(ToolsTabPage.TAG, '审批详情printObj==', res);
            if (res.msgCode == '1') {
                this.navCtrl.push(HtbgInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }
        }, error => {
            this.showError('数据请求失败');
        })
    }
    /**
     * 审批详情(合同签订)
     * */
    getHtgldHbspDetail(data,spType){

        this.sys.htgldbspDetail(data).subscribe((res: any) => {
            Log.printObj(ToolsTabPage.TAG, '审批详情(合同签订)printObj==', res);
            if (res.msgCode === '1') {
                this.navCtrl.push(HtqdInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }
        }, error => {
            this.showError('数据请求失败');
        })
    }
    /**
     * 审批详情(采购需求)
     * */
    getCgxqdbspDetail(data,spType){

        this.sys.htgldbspDetail(data).subscribe((res: any) => {
            Log.printObj(ToolsTabPage.TAG, '审批详情(采购需求)printObj==', res);
            if (res.msgCode == '1') {
                this.navCtrl.push(CgInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }
        }, error => {
            this.showError('数据请求失败');
        })
    }
    //资金申请
    getZjCheckDetail(data,spType) {

        this.sys.dbspzjDetails(data).subscribe((res: any) => {
            Log.printObj(ToolsTabPage.TAG, '资金待审详细printObj==', res);
            if (res.msgCode == '1') {
                this.navCtrl.push(ZjInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }
        }, error => {
            this.showError('数据请求失败');
        })
    }
    //执行申请
    getZxCheckDetail(data,spType) {

        this.sys.dbspzxDetails(data).subscribe((res: any) => {
            if (res.msgCode == '1') {
                Log.printObj(ToolsTabPage.TAG, "执行待审详细data==",res);
                this.navCtrl.push(ZxInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }

        }, error => {
            this.showError('数据请求失败');
        })
    }
    close(){
        this.faPiaoBool=false;
    }

}
